#include "KeyboardEvent.h"

namespace Lame3d
{
	KeyEvent::KeyEvent(SDL_KeyboardEvent evt)
			: key(evt.keysym.sym), modifiers(evt.keysym.mod),
				pressed(evt.type==SDL_KEYDOWN), character((char)(evt.keysym.unicode & 0x00FF))
	{
		modifiers = (SDLMod)(!KMOD_NUM & modifiers);
	}
	
	KeyEvent::KeyEvent(SDLKey k, SDLMod mod, bool p)
			: key(k), modifiers(mod), pressed(p), character(k)
	{
		modifiers = (SDLMod)(!KMOD_NUM & modifiers);
	}
	
	KeyEvent::~KeyEvent()
	{
	}
	
	bool KeyEvent::Pressed() const
	{
		return pressed;
	}
	
	char KeyEvent::Character() const
	{
		return character;
	}
	
	SDLKey KeyEvent::Key() const
	{
		return key;
	}
	
	SDLMod KeyEvent::Modifiers() const
	{
		return modifiers;
	}
	
	bool KeyEvent::NumLock() const
	{
		return false;
	}
	
	bool KeyEvent::CapsLock() const
	{
		return modifiers & KMOD_CAPS;
	}
	
	bool KeyEvent::LeftControl() const
	{
		return modifiers & KMOD_LCTRL;
	}
	
	bool KeyEvent::RightControl() const
	{
		return modifiers & KMOD_RCTRL;
	}
	
	bool KeyEvent::Control() const
	{
		return modifiers & KMOD_CTRL;
	}
	
	bool KeyEvent::LeftShift() const
	{
		return modifiers & KMOD_LSHIFT;
	}
	
	bool KeyEvent::RightShift() const
	{
		return modifiers & KMOD_RSHIFT;
	}
	
	bool KeyEvent::Shift() const
	{
		return modifiers & KMOD_SHIFT;
	}
	
	bool KeyEvent::LeftAlt() const
	{
		return modifiers & KMOD_LALT;
	}
	
	bool KeyEvent::RightAlt() const
	{
		return modifiers & KMOD_RALT;
	}
	
	bool KeyEvent::Alt() const
	{
		return modifiers & KMOD_ALT;
	}
	
	bool KeyEvent::IsText() const
	{
		if(	(character >= ' ' && character <= '}')) {
			return !Control() && !Alt();
		}
		return false;
	}
	
	bool KeyEvent::operator==(const KeyEvent &e) const
	{
		return (key == e.key) && (modifiers == e.modifiers) && (pressed == e.pressed);
	}
	
	bool KeyEvent::operator<(const KeyEvent &e) const
	{
		if(key == e.key) {
			if(modifiers == e.modifiers) {
				return pressed && !e.pressed; // hackery, might not work.
			} else {
				return modifiers < e.modifiers;
			}
		} else {
			return key < e.key;
		}
	}
	
	std::string KeyEvent::String() const
	{
		std::string buf("");
		if(NumLock()) {
			buf += "NUM+";
		}
		if(CapsLock()) {
			buf += "CAPS+";
		}
		if(LeftControl()) {
			buf += "LCTRL+";
		}
		if(RightControl()) {
			buf += "RCTRL+";
		}
		if(LeftShift()) {
			buf += "LSHIFT+";
		}
		if(RightShift()) {
			buf += "RSHIFT+";
		}
		if(RightAlt()) {
			buf += "RALT+";
		}
		if(LeftAlt()) {
			buf += "LALT+";
		}
		if(IsText()) {
			buf += " (";
			buf += character;
			buf += ")";
		} else {
			char b2[25];
			sprintf(b2, "%d", key);
			buf += b2;
		}
		if(pressed) {
			buf += " DOWN";
		} else {
			buf += " UP";
		}
		return buf;
	}
	
	// KeyboardCallback def'n
	KeyboardCallback::KeyboardCallback(const std::string &n)
			: kcName(n)
	{
	}
	
	KeyboardCallback::~KeyboardCallback()
	{
	}
	
	const std::string& KeyboardCallback::Name() const
	{
		return kcName;
	}
	
	bool KeyboardCallback::operator==(const KeyboardCallback &kc) const
	{
		return kcName == kc.kcName;
	}
}

// eof //
